#!/bin/bash

########################################################################
## This is a very basic script to update and "install" usp            ##
## This file should be placed in the first ubuntu-system-panel folder ##
## i.e. <your dir>/*ubuntu-system-panel*/ubuntu-system-panel/<blah>   ##
## v 0.4                                             PsyberOne - 2007 ##
########################################################################

update_usp ()
{
   SVN_LOCAL=$(svn info | grep 'Revision:')
   SVN_LOCAL=${SVN_LOCAL/"Revision: "/""}
   SVN_REMOTE=$(svn info http://ubuntu-system-panel.googlecode.com/svn/trunk | grep 'Revision:')
   SVN_REMOTE=${SVN_REMOTE/"Revision: "/""}

if (( "$SVN_REMOTE" > "$SVN_LOCAL" ))
   then
      echo "Updates from $SVN_LOCAL to $SVN_REMOTE:"
      svn log -r $SVN_REMOTE:$SVN_LOCAL

      echo "Fetching updates..."
      svn up > /dev/null 2>&1
   
   else
      echo "USP is up-to-date"
      exit
fi

}

install_usp ()
{
   echo "Creating directories if needed..."
   sudo install -d -m 755 /usr
   sudo install -d -m 755 /usr/bin
   sudo install -d -m 755 /usr/lib
   sudo install -d -m 755 /usr/lib/bonobo
   sudo install -d -m 755 /usr/lib/bonobo/servers
   sudo install -d -m 755 /usr/lib/python2.4
   sudo install -d -m 755 /usr/lib/python2.4/site-packages
   sudo install -d -m 755 /usr/lib/python2.4/site-packages/usp
   sudo install -d -m 755 /usr/lib/python2.4/site-packages/usp/locale
   sudo install -d -m 755 /usr/lib/python2.4/site-packages/usp/locale/en_GB
   sudo install -d -m 755 /usr/lib/python2.4/site-packages/usp/locale/en_GB/LC_MESSAGES
   sudo install -d -m 755 /usr/lib/python2.4/site-packages/usp/locale/en_GB/po
   sudo install -d -m 755 /usr/lib/python2.4/site-packages/usp/locale/en_US
   sudo install -d -m 755 /usr/lib/python2.4/site-packages/usp/locale/en_US/LC_MESSAGES
   sudo install -d -m 755 /usr/lib/python2.4/site-packages/usp/locale/en_US/po
   sudo install -d -m 755 /usr/lib/python2.4/site-packages/usp/locale/de_DE
   sudo install -d -m 755 /usr/lib/python2.4/site-packages/usp/locale/de_DE/LC_MESSAGES
   sudo install -d -m 755 /usr/lib/python2.4/site-packages/usp/locale/de_DE/po
   sudo install -d -m 755 /usr/lib/python2.4/site-packages/usp/locale/pl_PL
   sudo install -d -m 755 /usr/lib/python2.4/site-packages/usp/locale/pl_PL/LC_MESSAGES
   sudo install -d -m 755 /usr/lib/python2.4/site-packages/usp/locale/pl_PL/po
   sudo install -d -m 755 /usr/lib/python2.4/site-packages/usp/locale/ru_RU
   sudo install -d -m 755 /usr/lib/python2.4/site-packages/usp/locale/ru_RU/LC_MESSAGES
   sudo install -d -m 755 /usr/lib/python2.4/site-packages/usp/locale/ru_RU/po
   sudo install -d -m 755 /usr/lib/python2.4/site-packages/usp/plugins
   sudo install -d -m 755 /usr/share
   sudo install -d -m 755 /usr/share/usp
   echo "Installing USP 2.00+svn_$SVN_REMOTE..."
   sudo cp ./ubuntu-system-panel/usr/bin/* /usr/bin/
   sudo cp ./ubuntu-system-panel/usr/lib/bonobo/servers/* /usr/lib/bonobo/servers/
   sudo cp ./ubuntu-system-panel/usr/lib/python2.4/site-packages/usp/plugins/* /usr/lib/python2.4/site-packages/usp/plugins/
   sudo cp -r ./ubuntu-system-panel/usr/lib/python2.4/site-packages/usp/locale/* /usr/lib/python2.4/site-packages/usp/locale/
   sudo cp ./ubuntu-system-panel/usr/share/usp/* /usr/share/usp/
   echo "Installation is complete!"
   echo "Remove and re-add USP from panel to register updates."
   echo "However, you may need to logout or restart gnome-panel"
   echo "for updates to take affect."

}

uninstall_usp ()
{

   echo "Uninstalling USP..."
   sudo rm /usr/bin/usp
   sudo rm /usr/bin/uspconfig
   sudo rm /usr/lib/bonobo/servers/usp.server
   sudo rm -rf /usr/lib/python2.4/site-packages/usp
   sudo rm -rf /usr/share/usp
 
}

case "$1" in
   
   "install" | "Install" )
   
   if [ "$2" = "fresh" ]
      then      
         rm ~/.usp
         gconftool-2 --recursive-unset /apps/usp
   fi
   
   install_usp
   exit
   ;;

   "update" | "Update" | "" )
   
   update_usp
   install_usp
   exit
   ;;
   
   "uninstall" | "Uninstall" )
   
   if [ "$2" = "complete" ]
      then
         rm ~/.usp
         gconftool-2 --recursive-unset /apps/usp
   fi
   
   uninstall_usp
   exit
   ;;
   
esac
